const Ws = require('ws')

;
((Ws) => {

    const server = new Ws.Server({
        port: 8060
    })

    const init = () => {
        bindEvent();
    }

    function bindEvent() {
        server.on('open', handleOpen)
        server.on('close', handleClose)
        server.on('error', handleError)
        server.on('connection', handleConnection)

    }

    function handleOpen() {
        console.log('连接开启')

    }

    function handleClose() {
        console.log('连接关闭')

    }

    function handleError() {
        console.log('连接出错')

    }

    function handleConnection(ws) {
        console.log('连接Connection')
        ws.on('message', data => {
            data = JSON.parse(data)
            if (!data.msgType) {
                ws.uid = data.uid
                console.log('给ws赋值id走到这里来', data)
            } else {
                return
            }
        })


        ws.on('message', handleMessage)
    }


    function handleMessage(msg) {
        let data = JSON.parse(msg)
        console.log(data)
        let buffer = Buffer.from(msg, 'utf-8')
        var base64Str = buffer.toString('base64')
        let rawStr = Buffer.from(base64Str, 'base64').toString('utf-8')
        let receiveId = data.receive_id
        if (data.msgType === 2) { //这是私聊的信息
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }

        if (data.msgType === 3) {
            //这是好友请求
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }
        if (data.msgType === 4) {
            //这是同意好友请求
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }
        if (data.msgType === 5) {
            //这是拒绝好友请求
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }
        if (data.msgType === 6) {
            //这是向好友分享商品
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }
        if (data.msgType === 7) {
            //这是发送名片
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }
        if (data.msgType === 8) {
            //这是发送语音
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }
        if (data.msgType === 9) {
            //这是发送文件
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }
        if (data.msgType === 10) {
            //这是发送红包
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }
        if (data.msgType === 11) {
            //这是领取红包的提示
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }
        if (data.msgType === 12) {
            //这是订单消息
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }
        if (data.msgType === 13) {
            //这是订单推送消息
            server.clients.forEach(c => {
                if (receiveId === c.uid) {
                    c.send(rawStr)
                }
                return
            })
        }
        if (data.msgType === 14) {
            //这是活动推送
            server.clients.forEach(c => {
                c.send(rawStr)
            })
        }

    }

    init()

})(Ws)